package com.cecil.generate.model;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cecil.generate.exception.BusinessException;
import com.cecil.generate.util.StringUtil;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public enum PageParamSearchTypeEnum {

    EQ("eq", "等于") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.eq(column, value);
        }
    },
    NE("ne", "不等于") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.ne(column, value);
        }
    },
    GT("gt", "大于") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.gt(column, value);
        }
    },
    GE("ge", "大于等于") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.ge(column, value);
        }
    },
    LT("lt", "小于") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.lt(column, value);
        }
    },
    LE("le", "小于等于") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.le(column, value);
        }
    },
    BETWEEN("between", "之间") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.between(column, values[0], values[1]);
        }
    },
    NOT_BETWEEN("not_between", "不在之间") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.notBetween(column, values[0], values[1]);
        }
    },
    IN("in", "包含某些值") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.in(column, values);
        }
    },
    NOT_IN("not_in", "不包含某些值") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.notIn(column, values);
        }
    },
    LIKE("like", "模糊搜索") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.like(column, value);
        }
    },
    NOT_LIKE("not_like", "反向模糊搜索") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.notLike(column, value);
        }
    },
    LIKE_LEFT("like_left", "左边模糊搜索") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.likeLeft(column, value);
        }
    },
    LIKE_RIGHT("like_right", "右边模糊搜索") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.likeRight(column, value);
        }
    },
    IS_NULL("is_null", "为空") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.isNull(column);
        }
    },
    IS_NOT_NULL("is_not_null", "不为空") {
        @Override
        public <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values) {
            queryWrapper.isNotNull(column);
        }
    },
    ;

    public String type;
    public String desc;

    PageParamSearchTypeEnum(String type, String desc) {
        this.type = type;
        this.desc = desc;
    }

    public static PageParamSearchTypeEnum findByType(String type) {
        if (StringUtil.isEmpty(type)) {
            return null;
        }
        return Arrays.stream(values()).filter(item -> item.type.equals(type)).findFirst().orElse(null);
    }

    public static List<EnumVO> getAllEnums() {
        return Arrays.stream(values()).map(item -> new EnumVO(item.type, item.desc)).collect(Collectors.toList());
    }

    public abstract <T> void handle(QueryWrapper<T> queryWrapper, String column, Object value, Object[] values);

    public static <T> void buildWrapper(QueryWrapper<T> queryWrapper, List<PageParam.Param> params) {
        for (PageParam.Param param : params) {
            PageParamSearchTypeEnum typeEnum = PageParamSearchTypeEnum.findByType(param.getSearchType());
            if (null == typeEnum) {
                throw new BusinessException("searchType 参数错误");
            }
            typeEnum.handle(queryWrapper, param.getSearchType(), param.getValue(), param.getValues());
        }
    }

}
